package com.swu.ws.homework;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class app {
    public static void main(String[] args) {
        //下载指定位置的文本文件
        String sourceUrl="http://10.122.7.154/javaweb/data/images-url.txt";
        String targetUrl="C:\\Users\\xlc\\IdeaProjects\\Test1\\source\\text\\images-url.txt";

        try {
            Downloader.downloadFile(sourceUrl,targetUrl);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("text文本文件下载完成");


        //读取下载下来的文本文件
        String[] lines = new String[5];
        try {
            BufferedReader reader = new BufferedReader(new FileReader(targetUrl));
            String line;
            int num=0;
            while ((line = reader.readLine()) != null) {
                lines[num]=line;
                num++;
                System.out.println(line);
                System.out.println(num);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //将文本文件中的地址中的图片全部下载下来，并储存在指定位置
        String[] imgTargetUrl={
                "c:\\images\\www.swu.edu.cn\\xywh\\zhanbanzongban\\01.jpg",
                "c:\\images\\www.swu.edu.cn\\xywh\\xiaoshi01\\01.jpg",
                "c:\\images\\www.swu.edu.cn\\xywh\\xiaoshi06\\01.jpg",
                "c:\\images\\www.swu.edu.cn\\xywh\\xiaoshi08\\01.jpg",
                "c:\\images\\www.swu.edu.cn\\xywh\\xiaoshi10\\01.jpg",
        };
        System.out.println("文本文件全部储存在数组之中");
        for (int i=0;i< lines.length;i++) {
            System.out.println(lines[i]);
            System.out.println(i);
            try {
                Downloader.downloadFile(lines[i],imgTargetUrl[i]);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("图片已经全部下载");

        //读取文件大小，并根据文件大小进行排序
        //下面的循环是读取文件大小并将其储存在数组之中
        long[] imgBytes=new long[imgTargetUrl.length];
        for(int i=0;i< imgTargetUrl.length;i++){
            try {
                long fileSize = Files.size(Paths.get(imgTargetUrl[i]));
                imgBytes[i]=fileSize;
                System.out.println("File size: " + imgBytes[i] + " bytes");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //下面注释的代码为冒泡排序的测试
        /*long[] arr={1,4,6,7,8,3};
        long[] sortedArr=new long[arr.length];
        sorter sorter= new sorter();
        sortedArr=sorter.bubbleSort(arr);
        for (int i=0;i<sortedArr.length;i++){
            System.out.println(sortedArr[i]);
        }*///将大小和文件名储存在一个sortedmap之中
        SortedMap<Long,String> imgBytesMap=new TreeMap<>();
        for (int i=0;i<imgBytes.length;i++){
            imgBytesMap.put(imgBytes[i],imgTargetUrl[i]);
        }
        System.out.println("排序后的图片大小");
        for (Map.Entry<Long,String> entry:imgBytesMap.entrySet()){
            System.out.println(entry.getKey()+"  "+entry.getValue());
        }
        //将大小和文件名按照要求键入文本文件
        String textTargetUrl="c:\\images\\images-sorted.txt";
        for (Map.Entry<Long,String> entry:imgBytesMap.entrySet()){
            try {
                FileWriter fileWriter = new FileWriter(textTargetUrl,true);
                // 创建BufferedWriter对象，用于缓冲FileWriter的写入操作
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write(entry.getKey()+"  "+entry.getValue()+'\n');
                bufferedWriter.close();
                fileWriter.close();
                System.out.println("文本已成功写入文件: " + textTargetUrl);
            } catch (IOException e) {
                System.err.println("写入文件时发生错误: " + e.getMessage());
            }
        }
        //最后我们再读取一下文本文件，确保文本写入成功
        try {
            FileReader fileReader = new FileReader(textTargetUrl);
            // 创建BufferedWriter对象，用于缓冲FileWriter的写入操作
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            for (int i=0;i<imgBytes.length;i++) System.out.println(bufferedReader.readLine());
        } catch (IOException e) {
            System.err.println("写入文件时发生错误: " + e.getMessage());
        }
    }

}

